{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "54236f04",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "Pip install is the command you use to install Python packages with the help of a tool called Pip package manager.\n",
    "<br><br>Installing LangChain package\n",
    "</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "25059719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: langchain in c:\\users\\user\\anaconda3\\lib\\site-packages (0.0.181)\n",
      "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (4.0.2)\n",
      "Requirement already satisfied: numpy<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.23.5)\n",
      "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.8.4)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (3.8.4)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.4.39)\n",
      "Requirement already satisfied: PyYAML>=5.4.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (6.0)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (8.2.2)\n",
      "Requirement already satisfied: pydantic<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.10.8)\n",
      "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.2.4)\n",
      "Requirement already satisfied: requests<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.28.1)\n",
      "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (0.5.7)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.1.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
      "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.0.4)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n",
      "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n",
      "Requirement already satisfied: typing-inspect>=0.4.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.9.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n",
      "Requirement already satisfied: typing-extensions>=4.2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pydantic<2,>=1->langchain) (4.4.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (2022.12.7)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (1.26.14)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (3.4)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.1)\n",
      "Requirement already satisfied: packaging>=17.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (22.0)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (0.4.3)\n"
     ]
    }
   ],
   "source": [
    "!pip install langchain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da86f5fe",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "Installing Openai package, which includes the classes that we can use to communicate with Openai services\n",
    "<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a286b10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: Openai in c:\\users\\user\\anaconda3\\lib\\site-packages (0.27.7)\n",
      "Requirement already satisfied: aiohttp in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (3.8.4)\n",
      "Requirement already satisfied: requests>=2.20 in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (2.28.1)\n",
      "Requirement already satisfied: tqdm in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (4.64.1)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2022.12.7)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (1.26.14)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (3.4)\n",
      "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2.0.4)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.9.2)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.3)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (6.0.4)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.1)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (4.0.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (22.1.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\user\\anaconda3\\lib\\site-packages (from tqdm->Openai) (0.4.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install Openai"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d17c37be",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "Imports the Python built-in module called \"os.\"\n",
    "<br>This module provides a way to interact with the operating system, such as accessing environment variables, working with files and directories, executing shell commands, etc\n",
    "<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "93e917fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe5e9c51",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4d6c19f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model_name=\"text-davinci-003\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5e9e383c",
   "metadata": {},
   "outputs": [],
   "source": [
    "our_prompt = \"\"\"\n",
    "I love trips, and I have been to 6 countries. \n",
    "I plan to visit few more soon.\n",
    "\n",
    "Can you create a post for tweet in 10 words for the above?\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aa56b204",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I love trips, and I have been to 6 countries. \n",
      "I plan to visit few more soon.\n",
      "\n",
      "Can you create a post for tweet in 10 words for the above?\n"
     ]
    }
   ],
   "source": [
    "print(our_prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e8c01868",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nExploring the world! Been to 6 countries so far. More to come. #travel'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm(our_prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b544ff21",
   "metadata": {},
   "source": [
    "## Prompt Template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d3d1bd63",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f55beab7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fc4c87c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model_name=\"text-davinci-003\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4768461b",
   "metadata": {},
   "source": [
    "### Using F-String"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b5024d4",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "F-String is a Python feature that allows easy string formatting by placing variables inside curly braces within a string, making code more readable and efficient.\n",
    "    \n",
    "<br>\n",
    "__Code:__\n",
    "<br>\n",
    "name = \"Alice\"\n",
    "<br>\n",
    "age = 25\n",
    "<br>\n",
    "message = f\"My name is {name} and I am {age} years old.\"\n",
    "<br>\n",
    "print(message)\n",
    "<br><br>\n",
    "__Output:__\n",
    "<br>\n",
    "My name is Alice and I am 25 years old.\n",
    "<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "23109c05",
   "metadata": {},
   "outputs": [],
   "source": [
    "wordsCount=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d9ce8bba",
   "metadata": {},
   "outputs": [],
   "source": [
    "our_text = \"I love trips, and I have been to 6 countries. I plan to visit few more soon.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "70c0bfcb",
   "metadata": {},
   "outputs": [],
   "source": [
    "our_prompt = f\"\"\"\n",
    "{our_text}\n",
    "\n",
    "Can you create a post for tweet in {wordsCount} words for the above?\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dccfddcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I love trips, and I have been to 6 countries. I plan to visit few more soon.\n",
      "\n",
      "Can you create a post for tweet in 3 words for the above?\n"
     ]
    }
   ],
   "source": [
    "print (our_prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "38f1eef6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Travel dreams come true!'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm(our_prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a4fd394",
   "metadata": {},
   "source": [
    "### Using Prompt Template"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0264f268",
   "metadata": {},
   "source": [
    "<font color='green'>Prompt templates helps us in keeping our code neat and clean when we are building more complex <font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d81d00a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"\n",
    "{our_text}\n",
    "\n",
    "Can you create a post for tweet in {wordsCount} words for the above?\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b1b26b1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"wordsCount\",\"our_text\"],\n",
    "    template=template,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4b569cc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prompt = prompt.format(wordsCount='3',our_text=\"I love trips, and I have been to 6 countries. I plan to visit few more soon.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "68c72bc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I love trips, and I have been to 6 countries. I plan to visit few more soon.\n",
      "\n",
      "Can you create a post for tweet in 3 words for the above?\n"
     ]
    }
   ],
   "source": [
    "print (final_prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4c0f863b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')).\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Exploring the world!\n"
     ]
    }
   ],
   "source": [
    "print (llm(final_prompt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10cfaaf8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
